package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author: Sabrina
 * @Date:2025/06/13/17:11
 * @Description:
 */
@Mapper
public interface AddressBookMapper {
    /**
     * 查询当前登录用户的所有地址信息
     * @return
     */
    @Select("select * FROM address_book where user_id =#{currentId}")
    List<AddressBook> getAddresses(Long currentId);


    /**
     * 查询默认地址
     * @param currentId
     * @return
     */
    @Select("select * FROM address_book where is_default = 1 and user_id =#{currentId} ")
    AddressBook getDefaultAddress(Long currentId);

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @Select("select * FROM address_book where id = #{id}")
    AddressBook getAddressById(Long id);

    /**
     * 新增地址
     * @param addressBook
     */
    @Insert("insert into address_book" +
            "        (user_id, consignee, phone, sex, province_code, province_name, city_code, city_name, district_code," +
            "         district_name, detail, label, is_default)" +
            "        values (#{userId}, #{consignee}, #{phone}, #{sex}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}," +
            "                #{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void addAddr(AddressBook addressBook);

    /**
     * 修改地址
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     * @param id
     */
    @Delete("DELETE FROM address_book WHERE id = #{id}")
    void delete(Integer id);

    /**
     * 把当前用户的所有地址设置为非默认
     */
    @Update("update address_book set is_default = 0 where user_id = #{userId}")
    void updateIsDefaultByUserId(Long userId);
}
